﻿using System;
using TripleGame.Core;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TripleGame.RandomEngine
{
    public class TripleGameRandomEngine : ITripleGameEngine
    {
        private const char EMPTY_CELL = ' ';

        public string EngineName
        {
            get { return "Rnd Improved"; }
        }

        public void PerformMove(char[,] board, char playerLetter, out int startX, out int startY, out int endX, out int endY)
        {
            startX = 0;
            startY = 0;
            endX = 0;
            endY = 0;
            Random rnd = new Random();
            int emptyCells = 3;
            while (emptyCells != 0)
            {
                int turns = 0;
                while (turns < 10000)
                {
                    startX = rnd.Next() % board.GetLength(1);
                    startY = rnd.Next() % board.GetLength(0);

                    bool rightMoveValid = IsMoveValid(board, playerLetter, startY, startX, 1, 0, emptyCells);
                    if (rightMoveValid)
                    {
                        endX = startX + 2;
                        endY = startY;
                        return;
                    }

                    bool downMoveValid = IsMoveValid(board, playerLetter, startY, startX, 0, 1, emptyCells);
                    if (downMoveValid)
                    {
                        endX = startX;
                        endY = startY + 2;
                        return;
                    }

                    bool downRightMoveValid = IsMoveValid(board, playerLetter, startY, startX, 1, 1, emptyCells);
                    if (downRightMoveValid)
                    {
                        endX = startX + 2;
                        endY = startY + 2;
                        return;
                    }

                    bool downLeftMoveValid = IsMoveValid(board, playerLetter, startY, startX, -1, 1, emptyCells);
                    if (downLeftMoveValid)
                    {
                        endX = startX - 2;
                        endY = startY + 2;
                        return;
                    }
                    turns++;
                }
                emptyCells--;
            }
        }

        private bool IsMoveValid(char[,] board, char playerLetter,
            int startRow, int startCol, int stepX, int stepY, int emptyCells)
        {
            int row = startRow;
            int col = startCol;
            int numberOfEmptyCellsFilled = 0;
            for (int i = 1; i <= 3; i++)
            {
                if (row < 0 || row >= board.GetLength(0) ||
                    col < 0 || col >= board.GetLength(1))
                {
                    return false;
                }

                char currentCellUpcase = Char.ToUpperInvariant(board[row, col]);
                char playerLetterUpcase = Char.ToUpperInvariant(playerLetter);
                if (currentCellUpcase == EMPTY_CELL)
                {
                    numberOfEmptyCellsFilled++;
                }
                else if (currentCellUpcase != playerLetterUpcase)
                {
                    return false;
                }
                row += stepY;
                col += stepX;
            }

            if (numberOfEmptyCellsFilled < emptyCells)
            {
                return false;
            }

            return true;
        }
    }
}
